package org.edu.modules.center.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.edu.common.api.vo.Result;
import org.edu.common.aspect.annotation.AutoLog;
import org.edu.common.system.base.controller.BaseController;
import org.edu.common.system.query.QueryGenerator;
import org.edu.common.system.vo.LoginUser;
import org.edu.common.util.TimeStampUtil;
import org.edu.modules.center.entity.VeKfcFavor;
import org.edu.modules.center.service.IVeKfcFavorService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Api(tags={"课程收藏"})
@RestController
@RequestMapping({"/center/veKfcFavor"})
public class VeKfcFavorController
        extends BaseController<VeKfcFavor, IVeKfcFavorService>
{
    private static final Logger log = LoggerFactory.getLogger(VeKfcFavorController.class);
    @Autowired
    private IVeKfcFavorService veKfcFavorService;

    @AutoLog("课程收藏-当前用户收藏的课程分页查询")
    @ApiOperation(value="当前用户收藏的课程分页查询", notes="课程收藏-当前用户收藏的课程分页查询")
    @PostMapping({"/list"})
    public Result<?> queryPageList(VeKfcFavor veKfcFavor, @RequestParam(name="pageNo", defaultValue="1") Integer pageNo, @RequestParam(name="pageSize", defaultValue="10") Integer pageSize, HttpServletRequest req)
    {
        if (veKfcFavor.getUserid().equals(Integer.valueOf(0)))
        {
            LoginUser sysUser = (LoginUser)SecurityUtils.getSubject().getPrincipal();
            veKfcFavor.setUserid(sysUser.getUserId());
        }
        QueryWrapper<VeKfcFavor> queryWrapper = QueryGenerator.initQueryWrapper(veKfcFavor, req.getParameterMap());

        Page<Map<String, Object>> page = new Page(pageNo.intValue(), pageSize.intValue());
        queryWrapper.setEntity(veKfcFavor);
        IPage<Map<String, Object>> pageList = this.veKfcFavorService.pageMaps(page, queryWrapper);
        if (pageList == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(pageList);
    }

    @AutoLog("课程收藏-通过id查询")
    @ApiOperation(value="通过id查询", notes="课程收藏-通过id查询")
    @PostMapping({"/queryById"})
    public Result<?> queryById(@RequestParam(name="id", required=true) String id)
    {
        VeKfcFavor veKfcFavor = (VeKfcFavor)this.veKfcFavorService.getById(id);
        if (veKfcFavor == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(veKfcFavor);
    }

    @AutoLog("课程收藏-新增信息")
    @ApiOperation(value="新增信息", notes="课程收藏-新增信息")
    @PostMapping({"/add"})
    public Result<?> add(@RequestBody VeKfcFavor veKfcFavor)
    {
        LoginUser sysUser = (LoginUser)SecurityUtils.getSubject().getPrincipal();
        String createUserId = new String("abc");
        createUserId = sysUser.getUserId();
        veKfcFavor.setUserid(createUserId);
        Integer date = Integer.valueOf(TimeStampUtil.getNowSecondStamp());
        veKfcFavor.setAddtime(date);
        this.veKfcFavorService.save(veKfcFavor);
        return Result.OK("添加成功！");
    }
}
